const { sequelize } = require('../config/database');

async function rollbackCreatedByToCreatorId() {
  try {
    // 检查是否同时存在createdBy和creatorId字段
    const tableDescription = await sequelize.getQueryInterface().describeTable('parkpart_activities');
    
    if (tableDescription.createdBy && tableDescription.creatorId) {
      // 删除createdBy字段
      await sequelize.getQueryInterface().removeColumn('parkpart_activities', 'createdBy');
      
      console.log('Rollback completed successfully: removed createdBy field');
    } else if (tableDescription.createdBy && !tableDescription.creatorId) {
      // 如果只有createdBy字段，将其重命名为creatorId
      await sequelize.getQueryInterface().renameColumn('parkpart_activities', 'createdBy', 'creatorId');
      
      console.log('Rollback completed successfully: renamed createdBy to creatorId');
    } else {
      console.log('No createdBy field found, nothing to rollback');
    }
  } catch (error) {
    console.error('Rollback failed:', error);
  }
}

// 如果直接运行此文件，则执行回滚
if (require.main === module) {
  rollbackCreatedByToCreatorId().then(() => {
    console.log('Rollback process completed');
    process.exit(0);
  }).catch(error => {
    console.error('Rollback process failed:', error);
    process.exit(1);
  });
}

module.exports = rollbackCreatedByToCreatorId;